<template>
	<view class="content">
		<view class="pageHeader">
			展业设置
			<a class="generalBack" @click="backpage()" hover-class="none">
				<image src="/static/b-1.png" style="width: 18upx;height: 30upx;"></image>
			</a>
		</view>
		<view style="padding:20upx 0upx 22upx;">
			<view class="chooseitem flex aic jc-center" @click="choosegood">
				<view class="gtitle" :style="{color:mygood?'#333':'#999'}">{{mygood?mygood:'选择您想代理的商品'}}</view>
				<image src="../../static/down.png" style="width: 20upx;height: 10upx;margin-left: 40upx;"></image>
			</view>
			<view class="chooseitem flex aic jc-center"  @tap="showPicker('region')" >
				<view class="gtitle" :style="{color:result.result?'#333':'#999'}">{{result.result?result.result:'选择您希望授权的区域'}}</view>
				<image src="../../static/down.png" style="width: 20upx;height: 10upx;margin-left: 40upx;"></image>
			</view>
			<view class="rebtn" @click="sureadd">确认</view>
			<!-- 三级联动 -->
			<w-picker
				mode="region"
				:value="defaultRegion"
				default-type="id"
				:hide-area="showArea"
				:hide-city="showCity"
				:goods="goods"
				:verti="verti"
				@confirm="onConfirm($event,'region')"
				@cancel="onCancel"
				ref="region" 
			></w-picker>
		</view>
	</view>
</template>
<script>
	import postAjax from '../../API/postAjax.js';
	import wPicker from "@/components/w-picker/w-picker.vue"
	export default {
		data() {
			return {
				userToken: '', // token
				title:'',
				powerlist:[
				],
				page : 1,
				mygood:'',
				myarea:'',
				address:'',//省市区
				// 根据身份是否显示市和地区
				showArea:false,
				showCity:false,
				//三级联动
				result:{
					result:""
				},
				defaultProps:{
					label:"name",
					value:"id"
				},
				defaultRegion:["1","37","567"],
				// 身份信息
				verti:0,
				goods:null//good_id
			};
		},

		onLoad(options) {
			let that = this
			console.log(options)
			if(options.title){
				that.mygood = options.title
			}
			if(options.state){
				that.verti = options.state
				// if( options.state == 0){
				// }
				that.showArea = options.state !== '0'
				that.showCity =  options.state =='2'
				console.log(that.showCity,that.showArea)
			}
			if(options.goods_id){
				that.goods = options.goods_id
			}
			that.userToken = uni.getStorageSync('token')
			
		},
		components:{
			wPicker
		},
		methods: {
			// 返回上一页
			backpage() {
				uni.switchTab({
					url:'../tab/business'
				})
			},
			showPicker(type){
				this.$refs[type].show();
			},
			onConfirm(res,type){
				this.result=res;
				console.log(this.result)
			},
			onCancel(){
				this.defaultRegion=["1","37","567"]
			},
			choosegood(){
				uni.navigateTo({
					url:'showhall?state='+this.verti
				})
			},
			sureadd(){
				let that = this 
				if(that.goods === null){
					uni.showToast({
						icon: 'none',
						title: '请选择想代理的商品',
					})
					return
				}
				if(that.result.value === undefined){
					uni.showToast({
						icon: 'none',
						title: '请选择授权的区域',
					})
					return
				}
				let postdata = {
					token:that.userToken,
					goods_id:that.goods,
					exhibition_level_id:Number(that.verti)+1,
				}
				
				if(that.verti == '0'){
					postdata.province = that.result.value[0]
					postdata.city = that.result.value[1]
					postdata.area = that.result.value[2]
					if(that.result.obj.area.is_use==1){
						uni.showToast({
							icon: 'none',
							title: '您选择的区域已被授权，请重新选择',
						})
						that.result={
							result:""
						}
					}
				}
				if(that.verti == '1'){
					postdata.province = that.result.value[0]
					postdata.city = that.result.value[1]
					if(that.result.obj.city.is_use==1){
						uni.showToast({
							icon: 'none',
							title: '您选择的区域已被授权，请重新选择',
						})
						that.result={
							result:""
						}
					}
				}
				if(that.verti == '2'){
					postdata.province = that.result.value[0]
					if(that.result.obj.province.is_use==1){
						uni.showToast({
							icon: 'none',
							title: '您选择的区域已被授权，请重新选择',
						})
						that.result={
							result:""
						}
					}
				}
				console.log(postdata)
				postAjax('Promotebusiness/exhibitionsetting',postdata,function(data){
					if(data.code==0){
						uni.showToast({
							icon: 'none',
							title: '设置成功',
							duration: 1000
						})
						setTimeout(function(){
							uni.switchTab({
								url:'../tab/business'
							})
						},200)
					}else{
						uni.showToast({
							icon: 'none',
							title: '设置失败，请重试',
							duration: 1000
						})
						that.mygood = ''
						that.result ={
							result:''
						}
					}
				})
				
			},
		}
	};
</script>

<style>
	.content {
		min-height: 100vh;
		background-color: #f9f9f9;
	}
	.chooseitem{
		width: 500upx;
		padding: 24upx 0;
		border:1px solid rgba(29,132,232,1);
		border-radius:6upx;
		margin:52upx auto 0;
		font-size: 28upx;
	}
	.gtitle{max-width:350upx;overflow: hidden;
		white-space: nowrap;
		text-overflow: ellipsis;}
	
	.pageHeader {
		width: 100%;
		height: 120upx;
		background-color: #1d84e8;
		line-height: 120upx;
		text-align: center;
		font-size: 36upx;
		color: #fff;
		padding-top: 40upx;
		display: block;
	}

	.generalBack {
		position: absolute;
		top: 40upx;
		left: 30upx;
		display: block;
	}
	
	.rebtn {
		display: block;
		font-size: 30upx;
		border-radius: 6upx;
		width: 182upx;
		height: 60upx;
		line-height: 60upx;
		text-align: center;
		background-color: #FF4242;
		color: #fff;
		font-size: 28upx;
		margin: 90upx auto 0;
	}

	.rebtn::after {
		border: none;
	}
	.loading {
		text-align: center;
		line-height: 50px;
		font-size: 30upx;
		color: #666;
	}
</style>
